<?php
		include("dbconn.inc");
		include("dbfuc.inc");
		include("session.inc");		
		$user=$_SESSION["user"];
		$curid=$_SESSION["curid"];
		$sessionrandom=$_SESSION["sessionrandom"];
		$hidrandam=$_REQUEST["hidrandam"];
		$randam= rand(5, 100000);
		$today=date('d-m-y');

		//echo $sessionrandom;
		//echo "haii";
		//echo $hidrandam;
		$dbconnection=$_SESSION["dbconnection"];		
	if(session_is_registered("dbconnection") )
	{
				
		if($sessionrandom==$hidrandam)
		{	
			if(($user!="") && (($curid!="") || ($curid!=0)))
			{
				$errorflag=true;
			
?>
<html>
<head>
<title>Registration Step 3</title>
<style type="text/css">
<!--
.style19 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; color: #666666; }
.style20 {font-size: x-small;
	font-weight: bold;
	color: #000000;
}
.style23 {	color: #000000;
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: x-small;
	font-weight: bold;
}
-->
</style>
</head>

<body>
<?php
			
				$cmbRegStatus=$_SESSION['cmbRegStatus'];	
				$txtFnameReg=$_SESSION['txtFnameReg'];	
				$regtiksex=$_SESSION['regtiksex'];	
				$regCountry=$_SESSION['regCountry'];	
				$txtAddr1Reg=$_SESSION['txtAddr1Reg'];	
				$txtAddr2Reg=$_SESSION['txtAddr2Reg'];	
				$txtAddr3Reg=$_SESSION['txtAddr3Reg'];	
				$cmbStateReg=$_SESSION['cmbStateReg'];	
				$txtPinCodeReg=$_SESSION['txtPinCodeReg'];	
				$txtEmailReg=$_SESSION['txtEmailReg'];	
				$txtPhoneReg=$_SESSION['txtPhoneReg'];	
				$txtPhone2Reg=$_SESSION['txtPhone2Reg'];	
				
				$cBsnsName=$_SESSION['cBsnsName'];	
				$cPassword=$_SESSION['cPassword'];	
				$cUpline=$_SESSION['cUpline'];	
				$nUplineId=$_SESSION['nUplineId'];		
				$cRefferal=$_SESSION['cRefferal'];	
				$nRefferalId=$_SESSION['nRefferalId'];			
		
				$cSideId=$_SESSION['cSideId'];	
				$cSecretQuest=$_SESSION['cSecretQuest'];	
				$cAnsw=$_SESSION['cAnsw'];	
				$ccouponuser=$_SESSION['ccouponuser'];	
				$ccouponpassword=$_SESSION['ccouponpassword'];	
				$accountpin=$_SESSION['accountpin'];

				if($cSideId=="r")
				{
					$side="Right";
				}
				if($cSideId=="l")
				{
					$side="Left";
				}
			
						
				if(get_right_id($nUplineId))
					$nRightFlag=1;
				if(get_left_id($nUplineId))
					$nLeftFlag=1;
				if($nRightFlag==1 && $nLeftFlag==1)
				{
					$errorflag=false;
				}
				else
				{
					if($nRightFlag==1 && $cSideId=="r")
					{
						$errorflag=false;
					}
					if($nLeftFlag==1 && $cSideId=="l")
					{
						$errorflag=false;
					}
				}

      			if(isLoginExist($cBsnsName))  
				{
	        		 $errorflag=false;
				}
//finding next id value from maxtab

				$maxdata=mysql_query("select val as maxid from mlm_max_id where id='MLMMASTER' ");
				if($maximumdata=mysql_fetch_array($maxdata))
				{
					$maxid=$maximumdata['maxid'];
				}
				if($maxid >0)
				{
					$nextid=$maxid+1;
				}
				else
				{
					$nextid=1;
				}
							
				$maxdata=mysql_query("select count(1) count from mlm_geneology where n_left_id=$nextid or n_right_id=$nextid ");
				if($maximumdata=mysql_fetch_array($maxdata))
				{
					$count=$maximumdata['count'];
				}
				if($count >0)
				{
					$errorflag=false;
				}		
				
				$coupon_count=0;	
				$maxdata=mysql_query("select count(1) coupon_count from mlm_coupon_details where C_COUPON_NAME='$ccouponuser' AND C_COUPON_PASSWORD='$ccouponpassword' AND C_STATUS='N' ");
				if($maximumdata=mysql_fetch_array($maxdata))
				{
					$coupon_count=$maximumdata['coupon_count'];
				}
				if($coupon_count <= 0)
				{
					$errorflag=false;
				}
							
				if($errorflag)
				{
							
//updated the maxtab with next id
					$sqlmlm_max_id="update mlm_max_id set val='$nextid' where id='MLMMASTER' ";
//new member values are inserting to the master table

					$sqlmlm_master="insert into mlm_master(pn_id,n_int_id,n_ref_id,c_username,n_renewal_no,c_payment_id,
						c_enabled_ind,d_join,D_ACTIVATED,C_COUPON_USER,C_ACCOUT_PIN,C_TFLAG) values ('$nextid','$nRefferalId','$nUplineId','$cBsnsName','1','1','1',now(),now(),'$ccouponuser','$accountpin','Y')";
				 
//insert BC_LOGIN
					$sqlmlm_login="insert into mlm_login (PC_USERNAME,C_PASSWORD,C_SECRET_QUESTION,C_ANSWER) values ('$cBsnsName',
						md5('$cPassword'),'$cSecretQuest','$cAnsw')"; 
			 	 			
//ADDRESS_DTL
					$sqlmlm_address_dtl="insert into mlm_address_dtl(n_id,c_type,c_firm_name,c_fname,c_lname,c_addr1,c_addr2,
							   c_addr3,c_state,c_zip_code,c_tele_no,c_email,c_mobile) values 
							   ('$nextid','PAYA','$txtFnameReg','$txtFnameReg','$name','$txtAddr1Reg','$txtAddr2Reg',
							   '$txtAddr3Reg','$cmbStateReg','$txtPinCodeReg','$txtPhoneReg','$txtEmailReg','$mobile')";
//coupon details updation
					$sqlcouponupdate="update mlm_coupon_details set C_STATUS='Y' where C_COUPON_NAME='$ccouponuser' AND C_COUPON_PASSWORD='$ccouponpassword' ";
//insert mlm_geneology
					$sqlgeneologyinsert="insert into mlm_geneology(n_id) values('$nextid')";
					
 					if($cSideId=='l')
					{
					     $sqlgeneologyupdate="update mlm_geneology set n_left_id='$nextid' where n_id='$nUplineId'";
					}
					if($cSideId=='r')	  			 
					     $sqlgeneologyupdate="update mlm_geneology set n_right_id='$nextid' where n_id='$nUplineId'";
			 	 	
					
		//	try{
				//throw new Exception("Error occured in insertion");
					 mysql_query($sqlmlm_max_id);
					 mysql_query($sqlmlm_master);
					 mysql_query($sqlmlm_login);
					 mysql_query($sqlmlm_address_dtl);
					 mysql_query($sqlcouponupdate);
					 mysql_query($sqlgeneologyinsert);
					 mysql_query($sqlgeneologyupdate);
		//		}
		//		catch(Exception $e)
		//		{
			//	echo $e;
		//		echo 'Message: ' .$e->getMessage();
		//		}
	
//genology count updation
					$strCouponType = "A";
					$nextid = $nextid;
					$cUplineId = $n_ref_id;
					$strCouponType = '';
					$sqlSelect = mysql_query("select substr(C_COUPON_USER,1,1) as coupType from mlm_master where pn_id = '$nextid'");
					if($coupons = mysql_fetch_array($sqlSelect)) 
					{
						$strCouponType = $coupons['coupType'];
					}
		//		$maxdata=mysql_query("select substr(C_COUPON_USER,1,1) as coupType from bc_master where pn_id = '$nextid' ");
		//		if($maximumdata=mysql_fetch_array($maxdata)){
		//		$strCouponType=$maximumdata['coupType'];
		//		}

				
					if($strCouponType!="F") 
					{
						$childID = $nUplineId;
						$parentID = $nextid;
				//		echo $childID;
				//		echo $parentID;
						while($childID >=1) 
						{
				//	echo $strCouponType;
							$sqlGene = mysql_query("select n_left_id,n_right_id from mlm_geneology where n_id='$childID'");
							if($geneValues = mysql_fetch_array($sqlGene))
							{
					//	echo "HAIII";
								if($parentID == $geneValues['n_left_id']) 
								{
									$sqlGeneUp = "update mlm_geneology set n_left_count=n_left_count+1 where n_id='$childID'";
									//echo("mlm_geneology left count=+1 for id ". $childID );
		 						} 
								else if($parentID == $geneValues['n_right_id']) 
								{
									$sqlGeneUp = "update mlm_geneology set n_right_count=n_right_count+1  where n_id='$childID'";
									//echo"mlm_geneology right count=+1 for id ". childID );
			 					}
		 						mysql_query($sqlGeneUp);
							
								$sqlMlmmaster = mysql_query("select n_ref_id from mlm_master where pn_id='$childID'");
								if($mlmmasterValues = mysql_fetch_array($mlmmasterValues)) 
								{
									//	echo "HAIII";
									$childID = $geneValues['n_ref_id'];
								} 
								else
								{
									$childID =0;
								}
			 				}
								
							//$parentID = $childID;
						}
						
						//echo $childID;
						//echo $parentID;


						$sqlChildSel = mysql_query("select n_id as childid from mlm_geneology where n_left_id = '$parentID' or n_right_id = '$parentID'");
						//echo $sqlChildSel;
						if($childSel = mysql_fetch_array($sqlChildSel)) 
						{
							$childID =  $childSel['childid'];
						}

				
					}
			// ----dobt commented----
				//} 
					else 
					{
						echo("Free coupon. pls log");
					}	
	
					//	echo $nextid;
					//inserting new value to session
					$_SESSION["sessionrandom"]=$randam;
?>	
<table width="369" border="1" align="center" cellpadding="4" cellspacing="4" bordercolor="#666666">
  <tr>
    <td width="349" colspan="2" bgcolor="#F9F9F9" class="style19"><span class="style20"> Registration Details:-</span></td>
  </tr>
  <tr>
    <td height="30" class="style23">Registration completed successfully </td>
  </tr>
</table>
<table width="670" border="0" align="center" cellpadding="3" cellspacing="1" bordercolor="#FFFFFF" bgcolor="#FFFFFF">
  <tr bgcolor="#FFFFFF">
    <td height="74" colspan="2">&nbsp;</td>
    <td height="74">&nbsp;</td>
    <td height="74">&nbsp;</td>
  </tr>
  <tr>
    <td height="15" colspan="4"><font color="#000000" size="2" face="Verdana, Arial, Helvetica, sans-serif"> <strong><br>
      Congratulations and Welcome to .</strong></font><font face="Verdana, Arial, Helvetica, sans-serif"><br>
        <br>
        Dear &nbsp;<font color="#000000" size="2"><B><?php cFnameDelv ?></B>, </font></font></td>
  </tr>
  <tr>
    <td height="15" colspan="4"><font face="Verdana, Arial, Helvetica, sans-serif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font face="Geneva, Arial, Helvetica, sans-serif">&nbsp;&nbsp;Congratulations for having decided to be a <strong>xxxxx </strong> independent distributor. . </font></font></td>
  </tr>
  <tr>
    <td height="26" colspan="4"><br>
        <font face="Geneva, Arial, Helvetica, sans-serif">Rregistration details, </font></td>
  </tr>
  <tr>
    <td width="34" rowspan="6" bordercolor="#DADADA"><font size="3" face="Arial, Helvetica, sans-serif">&nbsp;</font><font size="3" face="Arial, Helvetica, sans-serif">&nbsp;</font><font size="3" face="Arial, Helvetica, sans-serif">&nbsp;</font></td>
    <td width="237" height="0" bordercolor="#DADADA"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b> Date of joining </b></font></td>
    <td width="5" bordercolor="#DADADA"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>:</b></font></td>
    <td width="375" height="0" bordercolor="#DADADA"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><b><?php echo $today ?></b></font></td>
  </tr>
  
  <tr >
    <td width="237" height="0" bordercolor="#DADADA"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><b>User Name</b></font></td>
    <td width="5" bordercolor="#DADADA"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>:</b></font></td>
    <td width="375" height="0"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><b><?php echo $cBsnsName ?></b></font></td>
  </tr>
  <tr >
    <td height="0" bordercolor="#DADADA"><b><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Placement</font></b></td>
    <td bordercolor="#DADADA"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>:</b></font></td>
    <td height="0"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><b><?php echo $side ?></b></font></td>
  </tr>
  <tr >
    <td height="28"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Upline 
      Username</b></font></b></td>
    <td><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>:</b></font></td>
    <td height="28"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><b><?php echo $cUpline ?></b></font></td>
  </tr>
  <tr >
    <td height="28"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Refferal Username                    </b></font></b></td>
    <td><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>:</b></font></td>
    <td height="28"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><b><?php refferal ?></b></font></td>
  </tr>
  <tr >
    <td colspan="3" bordercolor="#CCCCCC"><font color="#000000" size="2" face="Verdana, Arial, Helvetica, sans-serif"><b>Registration 
      Address</b></font></td>
  </tr>
  <tr>
    <td height="29" >&nbsp;</td>
    <td>&nbsp;</td>
    <td valign="top"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>:</b></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><font size="2"><b><font size="2"><b><font face="Verdana, Arial, Helvetica, sans-serif"><?php $cFnameDelv.'<br>'.$cAddr1Delv.'<br>'.$cAddr2Delv.'<br>'.$cAddr3Delv.'<br>'.$PinCodeDelv ?></font></b></font></b></font></font></td>
  </tr>
  <tr>
    <td height="29" colspan="4" >&nbsp;</td>
  </tr>
  <tr>
    <td height="29" colspan="4" >&nbsp;</td>
  </tr>
  <tr>
    <td height="29" colspan="4" ><strong><br>
    </strong></td>
  </tr>
  <tr>
    <td height="29" colspan="4" >&nbsp;</td>
  </tr>
</table>
<?php
				}
?>
</body>
</html>
<?php
			}   
			else
			{
				$errormsg="*Your session is expeired please login again";
				header("location: index.php?error=$errormsg");
			}			
		}
		else
		{
			//forword to registration
			header("location:registration_step1.php");
		}	
	}
	else
	{
		$errormsg="*Your session is expeired please login again";
		header("location: index.php?error=$errormsg");
	}
?>	

